package com.vitaf.cash.app.utils.helper

import com.vitaf.cash.app.BuildConfig
import com.vitaf.cash.app.VitafCashApplication
import com.vitaf.cash.app.configs.GombeCreditApiPath
import com.vitaf.cash.app.configs.GombeCreditApiUrl
import com.vitaf.cash.app.configs.GombeCreditApiUrl.addCommonParams
import com.vitaf.cash.app.configs.VitafConfigs
import com.vitaf.cash.app.request.HttpHelper
import com.vitaf.cash.app.request.IHttpCallback
import com.vitaf.cash.app.sdk.analyse.GombeCreditAnalyseBuilder
import com.vitaf.cash.app.sdk.analyse.GombeCreditAnalyseUtil
import com.vitaf.cash.app.sdk.dec.GombeCreditUserDecAsyn
import com.vitaf.cash.app.utils.shared.VitafSharedHp
import com.vitaf.cash.app.utils.shared.VitafSharedKey
import org.json.JSONObject

/**
 * TODO: Analysis Lib 初始化时机
 */
object GombeCreditAnalysisHelper {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        GombeCreditAnalyseBuilder.product = VitafConfigs.GOMBECREDIT_APPNAME
        GombeCreditAnalyseBuilder.appVersion = BuildConfig.VERSION_CODE.toString()
        GombeCreditAnalyseBuilder.appPckName = VitafConfigs.GOMBECREDIT_PACKAGENANME

        GombeCreditAnalyseBuilder.aesKey = VitafConfigs.takeGombeCreditSecretKey()
        GombeCreditAnalyseBuilder.aesIv = VitafConfigs.takeGombeCreditSecretIv()

        GombeCreditAnalyseBuilder.host = GombeCreditApiUrl.GOMBECREDIT_HOST
        GombeCreditAnalyseBuilder.zone = ""
        GombeCreditAnalyseBuilder.pathCommon = GombeCreditApiUrl.getRandomUrl()

        GombeCreditAnalyseBuilder.pathGoogleInstall = "/UFZoZXCOjAxfdHTC8G2CA"
        GombeCreditAnalyseBuilder.pathAdjustInstall = "/c0XkpVzPTJaOs45F9jxwg"
        GombeCreditAnalyseBuilder.pathFirebaseInstall = "/nYaCFTYDzdlpuk6MrF0EA"

        GombeCreditAnalyseUtil.init(VitafCashApplication.app)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        GombeCreditAnalyseBuilder.zone = VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_TIMEZONE)
        GombeCreditAnalyseUtil.init(VitafCashApplication.app)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        GombeCreditAnalyseBuilder.token = VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_TOKEN)
        GombeCreditAnalyseBuilder.userId = VitafSharedHp.takeGombeCreditSharedString(VitafSharedKey.GOMBECREDIT_USERID)
        GombeCreditAnalyseUtil.init(VitafCashApplication.app)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object TestDataLibInvoke {

    /**
     * 获取设备数据上传状态
     */
    fun takeGombeCreditDeviceInfoTypesAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path",GombeCreditApiPath.GOMBECREDIT_PATH.GOMBECREDIT_API_DEVICE_INFO_TYPES_PATH)
        }
        val httpUrl = GombeCreditApiUrl.takeRequestUrl()
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val deviceInfoTypes = obj.optString("deviceInfoTypes")
                            GombeCreditUserDecAsyn.startAsynTasks(deviceInfoTypes)
                        }
                    }
                    else -> {//失败全部上报
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }

            override fun onRequestFinish() {
            }
        })
    }
}